<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>画板</title>
    <link rel="stylesheet" href="/style.css">
</head>

<body>
    <canvas id='canvas' width="200" height="200"></canvas>
    <script>
        let canvas = document.getElementById('canvas');
        canvas.width = document.documentElement.clientWidth;
        canvas.height = document.documentElement.clientHeight;
        let ctx = canvas.getContext('2d');
        ctx.lineWidth = 6;
        ctx.lineCap = "round";
        ctx.fillStyle = 'black';
        ctx.strokeStyle = 'none';

        let painting = false;
        let last;

        var isTouchDevice = 'ontouchstart' in document.documentElement;

        function drawLine(x1, y1, x2, y2) {
            ctx.beginPath();

            ctx.moveTo(x1, y1);
            ctx.lineTo(x2, y2);
            ctx.closePath();
            ctx.stroke();
        }

        if (isTouchDevice) {
            canvas.ontouchstart = (e) => {
                let x = e.touches[0].clientX;
                let y = e.touches[0].clientY;
                last = [x, y];
            }
            canvas.ontouchmove = (e) => {
                let x = e.touches[0].clientX;
                let y = e.touches[0].clientY;
                drawLine(last[0], last[1], x, y);
                last = [x, y];
                // ctx.beginPath();
                // ctx.arc(x, y, 10, 0, 2 * Math.PI);
                // ctx.stroke();
                // ctx.fill();
            }

        } else {
            canvas.onmousedown = (e) => {
                painting = true;
                last = [e.clientX, e.clientY];
            }
            canvas.onmousemove = (e) => {
                if (painting === true) {
                    drawLine(last[0], last[1], e.clientX, e.clientY);
                    last = [e.clientX, e.clientY];
                    // ctx.beginPath();e.clientY
                    // ctx.arc(e.clientX, , 10, 0, 2 * Math.PI);
                    // ctx.stroke();
                    // ctx.fill();
                } else {
                    console('请移动端 移动鼠标绘画');
                }
            }
            canvas.onmouseup = () => {
                painting = false;
            }
        }
    </script>
</body>

</html>
